/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.relate;

/**
 * 九交模型
 * 
 * @author chunquedong
 * 
 */
public class IntersectionMatrix {
	// 集合
	public static final int Inner = 0;
	public static final int Border = 1;
	public static final int Outer = 2;
	// 维度
	public static final int EmptyDim = -1;
	public static final int PointDim = 0;
	public static final int LineDim = 1;
	public static final int AreaDim = 2;
	// 模式
	public static final String DisjointPattern = "FF*FF****";

	public static final String TouchesPattern1 = "FT*******";
	public static final String TouchesPattern2 = "F**T*****";
	public static final String TouchesPattern3 = "F***T****";

	public static final String CrossesPattern = "T*T******";
	public static final String CrossesPatternLL = "0********";

	public static final String WithinsPattern = "T*F**F***";

	public static final String OverlapPattern = "T*T***T**";
	public static final String OverlapPatternLL = "1*T***T**";
	// 交集矩阵
	private final int[][] matrix = new int[3][3];

	/**
	 * 初始值为EmptyDim
	 */
	public IntersectionMatrix() {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				matrix[i][j] = EmptyDim;
			}
		}
	}

	/**
	 * 设置IM，并同时设置了对称位置的值
	 * 
	 * @param r
	 * @param c
	 * @param dim
	 */
	public void set(int r, int c, int dim) {
		matrix[r][c] = dim;
	}

	public int get(int r, int c) {
		return matrix[r][c];
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				sb.append(matrix[i][j]);
			}
		}
		return sb.toString();
	}

	/**
	 * 匹配
	 * 
	 * @param m
	 * @return
	 */
	public boolean match(String m) {
		if (m.length() != 9) {
			throw new IllegalArgumentException();
		}

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				char c = m.charAt(i * 3 + j);
				boolean matched = matchEle(c, matrix[i][j]);
				if (!matched)
					return false;
			}
		}
		return true;
	}

	private boolean matchEle(char c, int dim) {
		if (c == 'T') {
			if (dim == 0 || dim == 1 || dim == 2)
				return true;
		} else if (c == 'F') {
			if (dim == -1)
				return true;
		} else if (c == '*') {
			return true;
		} else if (c == '0') {
			if (dim == 0)
				return true;
		} else if (c == '1') {
			if (dim == 1)
				return true;
		} else if (c == '2') {
			if (dim == 2)
				return true;
		} else {
			throw new IllegalArgumentException();
		}
		return false;
	}

	/**
	 * 交集维数
	 * 
	 * @param other
	 * @return
	 */
	public int intersects() {
		int max = matrix[0][0];

		if (max < matrix[0][1]) {
			max = matrix[0][1];
		}
		if (max < matrix[1][0]) {
			max = matrix[1][0];
		}
		if (max < matrix[1][1]) {
			max = matrix[1][1];
		}
		return max;
	}

	/**
	 * 取最大值合并
	 * 
	 * @param im
	 */
	public void unite(IntersectionMatrix im) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (matrix[i][j] < im.get(i, j))
					matrix[i][j] = im.get(i, j);
			}
		}
	}

	/**
	 * 对调，转置,返回新矩阵
	 * 
	 * @return
	 */
	public IntersectionMatrix reverse() {
		IntersectionMatrix tim = new IntersectionMatrix();
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				tim.set(j, i, matrix[i][j]);
			}
		}
		return tim;
	}
}